layui.config({
    base: '/home/js/util/'
}).use(['element','laypage','jquery','menu','table','form'],function(){
    element = layui.element,laypage = layui.laypage,
        $ = layui.$,menu = layui.menu,table = layui.table,form = layui.form;



    var rendData = function (data) {
        table.render({
            elem: '#request_table'
            ,cols: [[ //表头
                {type: 'checkbox', width: 30,fixed: 'left'}, //始终显示在左侧
                {field: 'reqId', title: 'ID', width:60,width:180, sort: true}
                ,{field: 'reqPath', title: '请求路径'}
                ,{field: 'needPermission', title: '权限控制', width:180, sort: true}
                ,{field: 'operateReason', title: '操作原因', width:160, sort: true}
                ,{field: 'remarks', title: '备注'}
                ,{
                    field: 'state',
                    title: '状态',
                    templet: function (field) {
                        return field.state == '1' ? "生效" : "失效";
                    },
                    width: 60
                },
                ,{field: 'cmosModifyTime', title: '修改时间', width: 160, sort: true}
                ,{field: 'createTime', title: '创建时间', width: 160, sort: true}
            ]],
            data: data,
            even: true,
            limit: data.length,
            limits: [10, 20, 30, 40, 50],
            toolbar: '#newsbar'
        });
    };

    var laypageTable = function (total) {
        laypage.render({
            elem: 'laypage',
            count: total,
            limit: searchParam.limit,
            curr: searchParam.page,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    searchParam.page = obj.curr;
                    searchParam.limit = obj.limit;
                    loadData();
                }
            }
        });
    };


    var searchParam = {
        req: {
            newsTitle:'',
            newsType:'',
            newsMedia:'',
            newsAnnounceTime:'',
            newsState:'',
        },
        page: 1,
        limit: 10
    };

    var loadData = function (page) {
        searchParam.page = CoreUtil.isEmpty(page) ? searchParam.page : page;
        //table数据加载
        CoreUtil.sendAjax("/requestController/getRequest", JSON.stringify(searchParam), function (res) {
            if (res.data && res.data.records != null) {
                //layer.msg('查询成功', {icon: 1});
                rendData(res.data.records);
                laypageTable(res.data.total);
            } else {
                layer.msg('查询失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };

    //查询请求
    form.on('submit(sreach)', function (data) {//查询操作
        searchParam.req.newsTitle = data.field.newsTitle;
        searchParam.req.newsType = data.field.newsType;
        searchParam.req.newsMedia = data.field.newsMedia;
        searchParam.req.newsAnnounceTime = data.field.newsAnnounceTime;
        if (data.field.state == "on"){
            searchParam.req.newsState = "1";
        }else {
            searchParam.req.newsState = "0";
        }
        loadData();
        return false;//表单提交不刷新,为true会刷新,默认刷新
    });



    //监听工具按钮事件
    table.on('toolbar(reqfilter)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);

        var data = checkStatus.data //获取选中行的数据
        var length = checkStatus.data.length; //获取选中行数量，可作为是否有选中行的条件
        switch(obj.event){
            case 'del':
                if (length <= 0){
                    layer.msg("至少要选择一条数据",{icon:5,time:1000});
                    break;
                }
                deleteNews(data);
                break;
            case 'forbidden': //禁用
                if (length <= 0){
                    layer.msg("至少要选择一条数据",{icon:5,time:1000});
                    break;
                }
                forbiddeneNews(data);
                break;
            case 'enable':
                if (length <= 0){
                    layer.msg("至少要选择一条数据",{icon:5,time:1000});
                    break;
                }
                enableNews(data);
                break;

        };
    });
//删除请求
    var deleteNews = function(data){

        if (data == null) return;
        /*主键*/
        var ids = new Array();
        $.each(data,function (index,value){
            ids.push(value.newsId);
        });

        //询问框

        layer.confirm('您确定要删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            //加载层
            layer.load();

            CoreUtil.sendAjax("/news/delete",JSON.stringify(ids),function (data){
                if ("0" == data.code){

                    layer.msg("删除成功！",{icon:1,time:1000});

                    loadData();
                }else {
                    layer.msg(data.msg,{icon:5,time:1000});
                }

            }, "POST", false, function (res) {
                layer.msg("系统异常");
            });
            //关闭弹出层
            layer.closeAll('loading');
        });


    };

    //取消展示新闻
    var forbiddeneNews = function(data){

        if (data == null) return;
        console.log(data);
        /*主键*/
        var ids = new Array();
        $.each(data,function (index,value){
            ids.push(value.newsId);
        });

        //询问框

        layer.confirm('您确定要屏蔽新闻吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            //加载层
            layer.load();

            CoreUtil.sendAjax("/news/forbiddene",JSON.stringify(ids),function (data){
                if ("0" == data.code){

                    layer.msg("操作成功！",{icon:1,time:1000});

                    loadData();
                }else {
                    layer.msg(data.msg,{icon:5,time:1000});
                }

            }, "POST", false, function (res) {
                layer.msg("系统异常");
            });
            //关闭弹出层
            layer.closeAll('loading');
        });


    };


    //开启新闻
    var enableNews = function(data){

        if (data == null) return;
        console.log(data);
        /*主键*/
        var ids = new Array();
        $.each(data,function (index,value){
            ids.push(value.newsId);
        });

        //询问框

        layer.confirm('您确定要开启展示新闻吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            //加载层
            layer.load();

            CoreUtil.sendAjax("/news/forbiddene",JSON.stringify(ids),function (data){
                if ("0" == data.code){

                    layer.msg("操作成功！",{icon:1,time:1000});

                    loadData();
                }else {
                    layer.msg(data.msg,{icon:5,time:1000});
                }

            }, "POST", false, function (res) {
                layer.msg("系统异常");
            });
            //关闭弹出层
            layer.closeAll('loading');
        });


    };

    loadData();
    menu.init();
})
